<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>

/** 
 * @memo 定时窗口 
 * @author mengqingyu
 */
var json; //参数
var wid='98%';
var com_ds_cron = new Ext.data.SimpleStore({ 
	fields:["valtext","name"],
	data:[
			['1','触发时间'],
			['2','<fmt:message key="label.trigger.cronExpression"/>']
		 ]
});
var comboBox_cron = new Ext.form.ComboBox({
		id:'comboBox_cron',
		anchor:'80%',
		selectOnFocus : true,	 
		hideLabel:true,
	    store:com_ds_cron,
	    triggerAction: "all",  //不加该语句选中某项后
	    mode: "local",   //动态需要	
		displayField: "name",
	    valueField: "valtext",
	    value:'1',
		listeners:{
			select:function(combo,record,index){
				if(record.get("valtext")=='1'){
					Ext.getCmp('cron').hide();
					Ext.getCmp('cron').setValue("* * * * * ? *");
					Ext.getCmp('shijian').show();
				}
				else{
					Ext.getCmp('shijian').hide();
					Ext.getCmp('cron').show();
				}
			}
		}
})
var com_ds_day = new Ext.data.SimpleStore({ 
	fields:["valtext","name"],
	data:[['1','1日'],['2','2日'],['3','3日'],['4','4日'],['5','5日'],['6','6日'],
            ['7','7日'],['8','8日'],['9','9日'],['10','10日'],['11','11日'],['12','12日'],
            ['13','13日'],['14','14日'],['15','15日'],['16','16日'],['17','17日'],['18','18日'],
            ['19','19日'],['20','20日'],['21','21日'],['22','22日'],['23','23日'],['24','24日'],
            ['25','25日'],['26','26日'],['27','27日'],['28','28日'],['29','29日'],['30','30日'],['31','31日'],['*','每日'],['?','无']]
});
var comboBox_day = new Ext.form.ComboBox({
	id:'dayId',
	anchor:wid,
	selectOnFocus : true,	 
	hideLabel:true,
    store:com_ds_day,
    triggerAction: "all",  //不加该语句选中某项后
    mode: "local",   //动态需要	
	displayField: "name",
    valueField: "valtext",
    value:'*',
	listeners: {
		"select": function (combo,record,index){ 
			if(record.get("valtext")=='?'){
				comboBox_week.setValue('1');
			}
			else{
				comboBox_week.setValue('?');
			}
		 }
	}	 
	});	
var com_ds_week = new Ext.data.SimpleStore({ 
	fields:["valtext","name"],
	data:[['1','每周1'],['2','每周2'],['3','每周3'],['4','每周4'],['5','每周5'],['6','每周6'],
            ['7','每周日'],['?','无']]
});
var comboBox_week = new Ext.form.ComboBox({
	id:'weekId',
	anchor:wid,
	selectOnFocus : true,	 
	hideLabel:true,
    store:com_ds_week,
    triggerAction: "all",  //不加该语句选中某项后
    mode: "local",   //动态需要	
	displayField: "name",
    valueField: "valtext",
    value:'?',
	listeners: {
		"select": function (combo,record,index){ 
			if(record.get("valtext")=='?'){
				comboBox_day.setValue('*');
			}
			else{
				comboBox_day.setValue('?');
			}
		 }
	}	   
	});				     
var com_ds_month = new Ext.data.SimpleStore({ 
	fields:["valtext","name"],
	data:[['1','1月'],['2','2月'],['3','3月'],['4','4月'],['5','5月'],['6','6月'],
            ['7','7月'],['8','8月'],['9','9月'],['10','10月'],['11','11月'],['12','12月'],['*','每月']]
});
var comboBox_month = new Ext.form.ComboBox({
	id:'monthId',
	anchor:wid,
	selectOnFocus : true,	 
	hideLabel:true,
    store:com_ds_month,
    triggerAction: "all",  //不加该语句选中某项后
    mode: "local",   //动态需要	
	displayField: "name",
    valueField: "valtext",
    value:'*'
	});	
var year=new Date().format("Y");
	var yearArray = new Array(); 
for(var i=0;i<11;i++){
	if(i!=10){
		yearArray[i]=new Array();
    	yearArray[i][0]=(year+i)+"年";
    	yearArray[i][1]=(year+i);
	}
	else{
		yearArray[i]=new Array();
    	yearArray[i][0]="每年";
    	yearArray[i][1]="*";		        		        	
	}
}
var com_ds_year = new Ext.data.SimpleStore({    
    fields:["name","valtext"],   
    data:yearArray  
});   
var comboBox_year = new Ext.form.ComboBox({
	id:'yearId',
	anchor:wid,
	selectOnFocus : true,	 
	hideLabel:true,
    store:com_ds_year,
    triggerAction: "all",  //不加该语句选中某项后
    mode: "local",   //动态需要	
	displayField: "name",
    valueField: "valtext",
    value:'*'
	});	
var com_ds_hour = new Ext.data.SimpleStore({ 
	fields:["valtext","name"],
	data:[['0','0时'],['1','1时'],['2','2时'],['3','3时'],['4','4时'],['5','5时'],['6','6时'],
            ['7','7时'],['8','8时'],['9','9时'],['10','10时'],['11','11时'],['12','12时'],
            ['13','13时'],['14','14时'],['15','15时'],['16','16时'],['17','17时'],['18','18时'],
            ['19','19时'],['20','20时'],['21','21时'],['22','22时'],['23','23时'],['*','每时']]
});
var comboBox_hour = new Ext.form.ComboBox({
	id:'hourId',
	anchor:wid,
	selectOnFocus : true,	 
	hideLabel:true,
    store:com_ds_hour,
    triggerAction: "all",  //不加该语句选中某项后
    mode: "local",   //动态需要	
	displayField: "name",
    valueField: "valtext",
    value:'*'
	});		
	var minuteArray = new Array(); 
for(var i=0;i<61;i++){
	if(i!=60){
		minuteArray[i]=new Array();
    	minuteArray[i][0]=(i)+"分";
    	minuteArray[i][1]=(i);
	}
	else{
		minuteArray[i]=new Array();
    	minuteArray[i][0]="每分";
    	minuteArray[i][1]="*";		        		        	
	}
}
var com_ds_minute = new Ext.data.SimpleStore({    
    fields:["name","valtext"],   
    data:minuteArray  
});   
var comboBox_minute = new Ext.form.ComboBox({
	id:'minuteId',
	anchor:wid,
	selectOnFocus : true,	 
	hideLabel:true,
    store:com_ds_minute,
    triggerAction: "all",  //不加该语句选中某项后
    mode: "local",   //动态需要	
	displayField: "name",
    valueField: "valtext",
    value:'*'
	});			
var formTimer = new Ext.FormPanel({
    frame:true,
    id:'formTimer',
    title: '执行调度 cron ',
    bodyStyle:'padding:5px 5px 0',
    frame:true,
    layout:'form',
        items: [{
            id:'startTime',
            xtype:'textfield',
            fieldLabel: '<fmt:message key="label.trigger.startTime"/>',
            name: 'startTime',
            anchor:'50%',
            readOnly:true,
            cls:'Wdate',
            listeners: {
                'focus': function(e) {
            		WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})
                }
            }
        },{
        	id:'stopTime',
            xtype:'textfield',
            fieldLabel: '<fmt:message key="label.trigger.stopTime"/>',
            name: 'stopTime',
            anchor:'50%',
            readOnly:true,
            cls:'Wdate',
            listeners: {
                'focus': function(e) {
            		WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})
                }
            }
        },{
        	layout:'column',
        	defaults:{layout:'form'},
        	items:[{	
    			columnWidth:.189,
    			items: [comboBox_cron]
    		},{	
    			columnWidth:.811,
    			items: [{
	                id:'shijian',
                	layout:'column',
                	defaults:{layout:'form'},
                	items:[{	
            			columnWidth:.15,
            			items: [comboBox_year]
            		},{	
            			columnWidth:.15,
            			items: [comboBox_month]
            		},{	
            			columnWidth:.15,
            			items: [comboBox_week]
            		},{	
            			columnWidth:.15,
            			items: [comboBox_day]
            		},{	
            			columnWidth:.15,
            			items: [comboBox_hour]
            		},{	
            			columnWidth:.15,
            			items: [comboBox_minute]
            		},{	
            			columnWidth:.1,
            			items: [{xtype:'label'}]
            		}]
                },{
    				id:'cron',
                    xtype:'textfield',
                    hideLabel:true,
                    name: 'cronExpression',
                    value:'* * * * * ? *',
                    allowBlank:false,
                    blankText:"cron表达式不能为空",
                    maxLength:20, 
                    maxLengthText:'cron表达式不能大于20个字符',
                    hidden:true,
                    emptyText:'<fmt:message key="hint.trigger.cronExpression"/>',
                    anchor:'88%'
                }]
    		}]
        }]     
});

//设置定时窗口
var winTimer_quartzTrigger = new Ext.ux.Window({
	title:'设置定时任务',
	width: 600,
	height:200,
	items: [formTimer],
	buttons:[{
		text:'保存',
		handler:function(){
			addTimer_quartzTrigger();
			winTimer_quartzTrigger.hide();
		}
	},{
		text:'删除',
		handler:function(){
			delTimer_quartzTrigger();
			winTimer_quartzTrigger.hide();
		}
	}]
});

//添加定时
function addTimer_quartzTrigger(){
	if(Ext.getCmp('comboBox_cron').getValue()=='1'){
		json.cronExpression = '0 '+Ext.getCmp('minuteId').getValue()+' '+Ext.getCmp('hourId').getValue()+' '+Ext.getCmp('dayId').getValue()+' '+Ext.getCmp('monthId').getValue()+' '+Ext.getCmp('weekId').getValue()+' '+Ext.getCmp('yearId').getValue();
//		json.cronLabel = Ext.getCmp('yearId').getRawValue()+' '+Ext.getCmp('monthId').getRawValue()+' '+Ext.getCmp('weekId').getRawValue()+' '+Ext.getCmp('hourId').getRawValue()+' '+Ext.getCmp('minuteId').getRawValue();
		Ext.getCmp('cron').setValue(json.cronExpression);
	}
	else{
		json.cronExpression = Ext.getCmp('cron').getValue();
	}
	json.jobClass = "org.quartz.jobs.TimerJobImpl";
	json.startTime = Ext.getCmp("startTime").getValue();
	json.stopTime = Ext.getCmp("stopTime").getValue();
	Ext.Ajax.request({             
	    url: '${pageContext.request.contextPath}/baseTimer.quartz?method=addTimer',    
        params: json,                  
        success: function(response, options) {   
          Ext.Msg.alert("提示","操作成功！"); 
      }        
	});   
}
function delTimer_quartzTrigger(){
	Ext.Ajax.request({             
	    url: '${pageContext.request.contextPath}/baseTimer.quartz?method=delTimer',    
        params: json,                  
        success: function(response, options) {   
          Ext.Msg.alert("提示","操作成功！");     
      }        
	});   
}
//方法入口读取记录如果没有显示默认
function quartzTrigger_show(name,group,beanName,jtaFlag,params){
	json = params;				//执行任务需要的参数
    json.name = name; 			//为任务别名，如套表名字可重复
    json.group = group; 		//组名称，例如四部组表管理器可重复
    json.beanName = beanName; 	//定时要执行的service配置名称等例如createReportService
    json.jtaFlag = jtaFlag;		//是否多事务并发 true为使用
	Ext.Ajax.request({             
	    url: '${pageContext.request.contextPath}/baseTimer.quartz?method=findTimer',    
        params: {name:name,group:group},                  
        success: function(response, options) {   
	      var responseArray = Ext.util.JSON.decode(response.responseText); 
	      var cronExpression = responseArray.cronExpression;
	      var startTime = responseArray.startTime;
	      var endTime = responseArray.endTime;
	      var state = responseArray.state;
	      winTimer_quartzTrigger.show();
	      if(state!="-1"){
	      	  var cronArray = cronExpression.split(" ");
		      Ext.getCmp('minuteId').setValue(cronArray[1]);
		      Ext.getCmp('hourId').setValue(cronArray[2]);
		      Ext.getCmp('dayId').setValue(cronArray[3]);
		      Ext.getCmp('monthId').setValue(cronArray[4]);
		      Ext.getCmp('weekId').setValue(cronArray[5]);
		      Ext.getCmp('yearId').setValue(cronArray[6]);
			  if(state=='0'){
			  	Ext.getCmp('formTimer').setTitle("执行调度 cron  状态：运行中    " + "<a href='#' onclick=\"javascript:startOrStopTimer()\">[暂停]</a>");
	          }
	          else if(state=='1'){
	         	Ext.getCmp('formTimer').setTitle("执行调度 cron  状态：暂停中    " + "<a href='#' onclick=\"javascript:startOrStopTimer()\">[恢复]</a>");
	          }
	      }
          else{
         	Ext.getCmp('formTimer').setTitle("执行调度 cron  状态：未设置" );
          	Ext.getCmp('formTimer').form.reset();
          }
      }        
	});  
}

function startOrStopTimer(){
	Ext.Ajax.request({             
	    url: '${pageContext.request.contextPath}/baseTimer.quartz?method=startOrStopTimer',  
	    params: json,   
        success: function(response, options) {   
	      var responseArray = Ext.util.JSON.decode(response.responseText); 
	      var state = responseArray.state;
     	  if(state=='0'){
	  	  	Ext.getCmp('formTimer').setTitle("执行调度 cron  状态：运行中    " + "<a href='#' onclick=\"javascript:startOrStopTimer()\">[暂停]</a>");
          }
          else if(state=='1'){
          	Ext.getCmp('formTimer').setTitle("执行调度 cron  状态：暂停中    " + "<a href='#' onclick=\"javascript:startOrStopTimer()\">[恢复]</a>");
          }
          else{
         	Ext.getCmp('formTimer').setTitle("执行调度 cron  状态：未设置" );
          }
      	}        
	});  
}